<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>custombuttonrenderer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_ui_custombuttonrenderer.js.html">custombuttonrenderer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A custom button renderer that uses CSS voodoo to render a
<a name="line17"></a> * button-like object with fake rounded corners.
<a name="line18"></a> *
<a name="line19"></a> * @author attila@google.com (Attila Bodis)
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.ui.CustomButtonRenderer&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.a11y.aria.Role&#39;);
<a name="line25"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.classlist&#39;);
<a name="line28"></a>goog.require(&#39;goog.string&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.ButtonRenderer&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.INLINE_BLOCK_CLASSNAME&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Custom renderer for {@link goog.ui.Button}s.  Custom buttons can contain
<a name="line36"></a> * almost arbitrary HTML content, will flow like inline elements, but can be
<a name="line37"></a> * styled like block-level elements.
<a name="line38"></a> *
<a name="line39"></a> * @constructor
<a name="line40"></a> * @extends {goog.ui.ButtonRenderer}
<a name="line41"></a> */
<a name="line42"></a>goog.ui.CustomButtonRenderer = function() {
<a name="line43"></a>  goog.ui.ButtonRenderer.call(this);
<a name="line44"></a>};
<a name="line45"></a>goog.inherits(goog.ui.CustomButtonRenderer, goog.ui.ButtonRenderer);
<a name="line46"></a>goog.addSingletonGetter(goog.ui.CustomButtonRenderer);
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * Default CSS class to be applied to the root element of components rendered
<a name="line51"></a> * by this renderer.
<a name="line52"></a> * @type {string}
<a name="line53"></a> */
<a name="line54"></a>goog.ui.CustomButtonRenderer.CSS_CLASS = goog.getCssName(&#39;goog-custom-button&#39;);
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/**
<a name="line58"></a> * Returns the button&#39;s contents wrapped in the following DOM structure:
<a name="line59"></a> *    &lt;div class=&quot;goog-inline-block goog-custom-button&quot;&gt;
<a name="line60"></a> *      &lt;div class=&quot;goog-inline-block goog-custom-button-outer-box&quot;&gt;
<a name="line61"></a> *        &lt;div class=&quot;goog-inline-block goog-custom-button-inner-box&quot;&gt;
<a name="line62"></a> *          Contents...
<a name="line63"></a> *        &lt;/div&gt;
<a name="line64"></a> *      &lt;/div&gt;
<a name="line65"></a> *    &lt;/div&gt;
<a name="line66"></a> * Overrides {@link goog.ui.ButtonRenderer#createDom}.
<a name="line67"></a> * @param {goog.ui.Control} control goog.ui.Button to render.
<a name="line68"></a> * @return {Element} Root element for the button.
<a name="line69"></a> * @override
<a name="line70"></a> */
<a name="line71"></a>goog.ui.CustomButtonRenderer.prototype.createDom = function(control) {
<a name="line72"></a>  var button = /** @type {goog.ui.Button} */ (control);
<a name="line73"></a>  var classNames = this.getClassNames(button);
<a name="line74"></a>  var attributes = {
<a name="line75"></a>    &#39;class&#39;: goog.ui.INLINE_BLOCK_CLASSNAME + &#39; &#39; + classNames.join(&#39; &#39;)
<a name="line76"></a>  };
<a name="line77"></a>  var buttonElement = button.getDomHelper().createDom(&#39;div&#39;, attributes,
<a name="line78"></a>      this.createButton(button.getContent(), button.getDomHelper()));
<a name="line79"></a>  this.setTooltip(
<a name="line80"></a>      buttonElement, /** @type {!string}*/ (button.getTooltip()));
<a name="line81"></a>  this.setAriaStates(button, buttonElement);
<a name="line82"></a>
<a name="line83"></a>  return buttonElement;
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Returns the ARIA role to be applied to custom buttons.
<a name="line89"></a> * @return {goog.a11y.aria.Role|undefined} ARIA role.
<a name="line90"></a> * @override
<a name="line91"></a> */
<a name="line92"></a>goog.ui.CustomButtonRenderer.prototype.getAriaRole = function() {
<a name="line93"></a>  return goog.a11y.aria.Role.BUTTON;
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Takes the button&#39;s root element and returns the parent element of the
<a name="line99"></a> * button&#39;s contents.  Overrides the superclass implementation by taking
<a name="line100"></a> * the nested DIV structure of custom buttons into account.
<a name="line101"></a> * @param {Element} element Root element of the button whose content
<a name="line102"></a> *     element is to be returned.
<a name="line103"></a> * @return {Element} The button&#39;s content element (if any).
<a name="line104"></a> * @override
<a name="line105"></a> */
<a name="line106"></a>goog.ui.CustomButtonRenderer.prototype.getContentElement = function(element) {
<a name="line107"></a>  return element &amp;&amp; /** @type {Element} */ (element.firstChild.firstChild);
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Takes a text caption or existing DOM structure, and returns the content
<a name="line113"></a> * wrapped in a pseudo-rounded-corner box.  Creates the following DOM structure:
<a name="line114"></a> *  &lt;div class=&quot;goog-inline-block goog-custom-button-outer-box&quot;&gt;
<a name="line115"></a> *    &lt;div class=&quot;goog-inline-block goog-custom-button-inner-box&quot;&gt;
<a name="line116"></a> *      Contents...
<a name="line117"></a> *    &lt;/div&gt;
<a name="line118"></a> *  &lt;/div&gt;
<a name="line119"></a> * Used by both {@link #createDom} and {@link #decorate}.  To be overridden
<a name="line120"></a> * by subclasses.
<a name="line121"></a> * @param {goog.ui.ControlContent} content Text caption or DOM structure to wrap
<a name="line122"></a> *     in a box.
<a name="line123"></a> * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
<a name="line124"></a> * @return {Element} Pseudo-rounded-corner box containing the content.
<a name="line125"></a> */
<a name="line126"></a>goog.ui.CustomButtonRenderer.prototype.createButton = function(content, dom) {
<a name="line127"></a>  return dom.createDom(&#39;div&#39;,
<a name="line128"></a>      goog.ui.INLINE_BLOCK_CLASSNAME + &#39; &#39; +
<a name="line129"></a>      goog.getCssName(this.getCssClass(), &#39;outer-box&#39;),
<a name="line130"></a>      dom.createDom(&#39;div&#39;,
<a name="line131"></a>          goog.ui.INLINE_BLOCK_CLASSNAME + &#39; &#39; +
<a name="line132"></a>          goog.getCssName(this.getCssClass(), &#39;inner-box&#39;), content));
<a name="line133"></a>};
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>/**
<a name="line137"></a> * Returns true if this renderer can decorate the element.  Overrides
<a name="line138"></a> * {@link goog.ui.ButtonRenderer#canDecorate} by returning true if the
<a name="line139"></a> * element is a DIV, false otherwise.
<a name="line140"></a> * @param {Element} element Element to decorate.
<a name="line141"></a> * @return {boolean} Whether the renderer can decorate the element.
<a name="line142"></a> * @override
<a name="line143"></a> */
<a name="line144"></a>goog.ui.CustomButtonRenderer.prototype.canDecorate = function(element) {
<a name="line145"></a>  return element.tagName == &#39;DIV&#39;;
<a name="line146"></a>};
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Check if the button&#39;s element has a box structure.
<a name="line151"></a> * @param {goog.ui.Button} button Button instance whose structure is being
<a name="line152"></a> *     checked.
<a name="line153"></a> * @param {Element} element Element of the button.
<a name="line154"></a> * @return {boolean} Whether the element has a box structure.
<a name="line155"></a> * @protected
<a name="line156"></a> */
<a name="line157"></a>goog.ui.CustomButtonRenderer.prototype.hasBoxStructure = function(
<a name="line158"></a>    button, element) {
<a name="line159"></a>  var outer = button.getDomHelper().getFirstElementChild(element);
<a name="line160"></a>  var outerClassName = goog.getCssName(this.getCssClass(), &#39;outer-box&#39;);
<a name="line161"></a>  if (outer &amp;&amp; goog.dom.classlist.contains(outer, outerClassName)) {
<a name="line162"></a>
<a name="line163"></a>    var inner = button.getDomHelper().getFirstElementChild(outer);
<a name="line164"></a>    var innerClassName = goog.getCssName(this.getCssClass(), &#39;inner-box&#39;);
<a name="line165"></a>    if (inner &amp;&amp; goog.dom.classlist.contains(inner, innerClassName)) {
<a name="line166"></a>      // We have a proper box structure.
<a name="line167"></a>      return true;
<a name="line168"></a>    }
<a name="line169"></a>  }
<a name="line170"></a>  return false;
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Takes an existing element and decorates it with the custom button control.
<a name="line176"></a> * Initializes the control&#39;s ID, content, tooltip, value, and state based
<a name="line177"></a> * on the ID of the element, its child nodes, and its CSS classes, respectively.
<a name="line178"></a> * Returns the element.  Overrides {@link goog.ui.ButtonRenderer#decorate}.
<a name="line179"></a> * @param {goog.ui.Control} control Button instance to decorate the element.
<a name="line180"></a> * @param {Element} element Element to decorate.
<a name="line181"></a> * @return {Element} Decorated element.
<a name="line182"></a> * @override
<a name="line183"></a> */
<a name="line184"></a>goog.ui.CustomButtonRenderer.prototype.decorate = function(control, element) {
<a name="line185"></a>  goog.asserts.assert(element);
<a name="line186"></a>
<a name="line187"></a>  var button = /** @type {goog.ui.Button} */ (control);
<a name="line188"></a>  // Trim text nodes in the element&#39;s child node list; otherwise madness
<a name="line189"></a>  // ensues (i.e. on Gecko, buttons will flicker and shift when moused over).
<a name="line190"></a>  goog.ui.CustomButtonRenderer.trimTextNodes_(element, true);
<a name="line191"></a>  goog.ui.CustomButtonRenderer.trimTextNodes_(element, false);
<a name="line192"></a>
<a name="line193"></a>  // Create the buttom dom if it has not been created.
<a name="line194"></a>  if (!this.hasBoxStructure(button, element)) {
<a name="line195"></a>    element.appendChild(
<a name="line196"></a>        this.createButton(element.childNodes, button.getDomHelper()));
<a name="line197"></a>  }
<a name="line198"></a>
<a name="line199"></a>  goog.dom.classlist.addAll(element,
<a name="line200"></a>      [goog.ui.INLINE_BLOCK_CLASSNAME, this.getCssClass()]);
<a name="line201"></a>  return goog.ui.CustomButtonRenderer.superClass_.decorate.call(this, button,
<a name="line202"></a>      element);
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Returns the CSS class to be applied to the root element of components
<a name="line208"></a> * rendered using this renderer.
<a name="line209"></a> * @return {string} Renderer-specific CSS class.
<a name="line210"></a> * @override
<a name="line211"></a> */
<a name="line212"></a>goog.ui.CustomButtonRenderer.prototype.getCssClass = function() {
<a name="line213"></a>  return goog.ui.CustomButtonRenderer.CSS_CLASS;
<a name="line214"></a>};
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * Takes an element and removes leading or trailing whitespace from the start
<a name="line219"></a> * or the end of its list of child nodes.  The Boolean argument determines
<a name="line220"></a> * whether to trim from the start or the end of the node list.  Empty text
<a name="line221"></a> * nodes are removed, and the first non-empty text node is trimmed from the
<a name="line222"></a> * left or the right as appropriate.  For example,
<a name="line223"></a> *    &lt;div class=&quot;goog-inline-block&quot;&gt;
<a name="line224"></a> *      #text &quot;&quot;
<a name="line225"></a> *      #text &quot;\n    Hello &quot;
<a name="line226"></a> *      &lt;span&gt;...&lt;/span&gt;
<a name="line227"></a> *      #text &quot; World!    \n&quot;
<a name="line228"></a> *      #text &quot;&quot;
<a name="line229"></a> *    &lt;/div&gt;
<a name="line230"></a> * becomes
<a name="line231"></a> *    &lt;div class=&quot;goog-inline-block&quot;&gt;
<a name="line232"></a> *      #text &quot;Hello &quot;
<a name="line233"></a> *      &lt;span&gt;...&lt;/span&gt;
<a name="line234"></a> *      #text &quot; World!&quot;
<a name="line235"></a> *    &lt;/div&gt;
<a name="line236"></a> * This is essential for Gecko, where leading/trailing whitespace messes with
<a name="line237"></a> * the layout of elements with -moz-inline-box (used in goog-inline-block), and
<a name="line238"></a> * optional but harmless for non-Gecko.
<a name="line239"></a> *
<a name="line240"></a> * @param {Element} element Element whose child node list is to be trimmed.
<a name="line241"></a> * @param {boolean} fromStart Whether to trim from the start or from the end.
<a name="line242"></a> * @private
<a name="line243"></a> */
<a name="line244"></a>goog.ui.CustomButtonRenderer.trimTextNodes_ = function(element, fromStart) {
<a name="line245"></a>  if (element) {
<a name="line246"></a>    var node = fromStart ? element.firstChild : element.lastChild, next;
<a name="line247"></a>    // Tag soup HTML may result in a DOM where siblings have different parents.
<a name="line248"></a>    while (node &amp;&amp; node.parentNode == element) {
<a name="line249"></a>      // Get the next/previous sibling here, since the node may be removed.
<a name="line250"></a>      next = fromStart ? node.nextSibling : node.previousSibling;
<a name="line251"></a>      if (node.nodeType == goog.dom.NodeType.TEXT) {
<a name="line252"></a>        // Found a text node.
<a name="line253"></a>        var text = node.nodeValue;
<a name="line254"></a>        if (goog.string.trim(text) == &#39;&#39;) {
<a name="line255"></a>          // Found an empty text node; remove it.
<a name="line256"></a>          element.removeChild(node);
<a name="line257"></a>        } else {
<a name="line258"></a>          // Found a non-empty text node; trim from the start/end, then exit.
<a name="line259"></a>          node.nodeValue = fromStart ?
<a name="line260"></a>              goog.string.trimLeft(text) : goog.string.trimRight(text);
<a name="line261"></a>          break;
<a name="line262"></a>        }
<a name="line263"></a>      } else {
<a name="line264"></a>        // Found a non-text node; done.
<a name="line265"></a>        break;
<a name="line266"></a>      }
<a name="line267"></a>      node = next;
<a name="line268"></a>    }
<a name="line269"></a>  }
<a name="line270"></a>};
</pre>


</body>
</html>
